package com.jsgt.greekfire.ui.trading.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.jsgt.greekfire.R
import com.jsgt.greekfire.factory.MarketViewModelFactory
import com.jsgt.greekfire.viewmodel.MarketViewModel
import com.jsgt.greekfire.viewmodel.TradingViewModel
import kotlinx.android.synthetic.main.fragment_introduce.*

private const val ARG_PARAM1 = "param1"

class SynopsisFragment : BaseFragment() {
    private lateinit var symbol: String

    private val tradingViewModel: TradingViewModel by lazy {
        ViewModelProvider(this).get(TradingViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            symbol = it.getString(ARG_PARAM1)!!
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_introduce
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String) =
            SynopsisFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                }
            }
    }

    override fun main() {
        tradingViewModel.coinIntroduceLiveData.observe(this, Observer {
            txvName.text = it.coinName
            txvCreateTime.text = it.createTime.substring(0, 10)
            txvTotal.text = it.issueAmount
            txvCirculation.text = it.circulationAmount
            txvPrice.text = it.crowdFunding
            txvWhitePaper.text = it.whitePaper
            txvOfficialWebsite.text = it.officialWebsite
            txvBlockQuery.text = it.blockQuery
            txvCoinIntroduce.text = it.coinIntroduce
        })
        tradingViewModel.errorLiveData.observe(this, Observer {
//            ToastUtils.show(it.toString())
        })
        if (symbol.contains(symbol)) {
            var coinName = symbol.substring(0, symbol.indexOf("_"));
            tradingViewModel.getCoinIntroduce(coinName)
        }
    }
}